<?php
include('/home/wwwroot/football.laulei.com/protected/extensions/simple_html_dom.php');
//获取并更新北京单场当前期的实时赔率，包括平均赔率、威廉希尔、澳门和立博
//截止时间是比赛前15分钟
class GetOddsInfosAllCommand extends CConsoleCommand  
{
    public function run($args) {
    	//$now=date('Y-m-d H:i:s',strtotime("-25 minute"));
    	$beginTime=date('Y-m-d H:i:s',strtotime("2009-01-01"));
    	$doneTime=date('Y-m-d H:i:s',strtotime("2014-04-16"));
    	while($beginTime<=$doneTime){
    		$endTime=date('Y-m-d H:i:s',strtotime("+1 day", strtotime($beginTime)));
    		printf("[beginTime=%s] [endTime=%s]\n", $beginTime, $endTime);
	    	$lstGameInfo = GameInfo::model()->findAllByAttributes(
			        array(),
			        $condition  = 'gametime >= :beginTime and gametime<:endTime',
			        $params     = array(
			                ':beginTime' => $beginTime,
			                ':endTime' => $endTime,
			        )
			);
			
			$betCompany = array(
				'2' => '立博',
				'3' => 'bet365',
				'4' => 'Interwetten',
				'5' => '澳门',
				'6' => '伟德',
				'8' => 'SNAI',
				'9' => 'Easybets(易胜博)',
				'11' => 'Bwin',
				'12' => 'Oddset',
				'14' => 'Coral',
				'15' => 'Eurobet',
				'16' => '10BET',
				'122' => '香港马会',
				'140' => 'Mansion88(明升)',
				'280' => 'Singbet(皇冠)',
				'291' => '优胜客',
				'293' => '威廉希尔',
				'348' => '188Bet(金宝博）',
				'1055' => 'PinnacleSports(平博)',
			);
			foreach ($lstGameInfo as $key => $value) {
				$gametype=$value->gametype;
				$discreteValueArray=array();//存储各个赔率，计算离散值
				//printf("[期号：%s] [序号：%s] [时间：%s] [类型：%s] [主队：%s] [让球：%s] [客队：%s] [主进：%s] [客进：%s] [胜：%s] [平：%s] [负：%s]\n", $value->issue, $value->gameindex, $value->gametime, $value->gametype, $value->hometeam, $value->rateball, $value->awayteam, $value->homegoal, $value->awaygoal, $value->euroodds3, $value->euroodds1, $value->euroodds0);
				$oddsUrl=sprintf("http://odds.500.com/fenxi/ouzhi-%s-show-2?ctype=1", $value->gameid);
				printf("%s\n", $oddsUrl);
				$htmlOdds = file_get_html($oddsUrl);
		    	if(!isset($htmlOdds)){
		    		continue;
		    	}
		    	//99家平均赔率
		    	$tmpAvg = $htmlOdds->find("td[id=avwinj1]", 0);
		    	if(!isset($tmpAvg)){
		    		$htmlOdds->clear();
					unset($htmlOdds);
	    			continue;
	    		}
		    	$avgOdds3=trim($htmlOdds->find("td[id=avwinj1]", 0)->plaintext);
		    	$avgOdds1=trim($htmlOdds->find("td[id=avdrawj1]", 0)->plaintext);
		    	$avgOdds0=trim($htmlOdds->find("td[id=avlostj1]", 0)->plaintext);
		    	$oddsInfo=new OddsInfo;
				$oddsInfo->gameid=$value->gameid;
				$oddsInfo->companyid='-1';
				$oddsInfo->companyname='平均值';
				$oddsInfo->odds3=$avgOdds3;
				$oddsInfo->odds1=$avgOdds1;
				$oddsInfo->odds0=$avgOdds0;
				$oddsInfo->writetime=date("Y-m-d H:i:s");
				$oddsInfo->save();
		    	printf("平均赔率 [编号：%s] [胜赔：%s] [平赔：%s] [负赔：%s]\n", $value->gameindex, $avgOdds3,$avgOdds1,$avgOdds0);

		    	//主流公司平均赔率
		    	$avgOdds3z=0;
		    	$avgOdds1z=0;
		    	$avgOdds0z=0;
		    	foreach ($betCompany as $key2 => $value2) {
		    		$tmpTR=$htmlOdds->find(sprintf("tr[id=tr2_%s]", $key2), 0);
		    		if(!isset($tmpTR)){
		    			continue;
		    		}
		    		$odds3=$tmpTR->find("td", 0)->plaintext;
		    		$odds1=$tmpTR->find("td", 1)->plaintext;
		    		$odds0=$tmpTR->find("td", 2)->plaintext;
		    		$oddsInfo=new OddsInfo;
					$oddsInfo->gameid=$value->gameid;
					$oddsInfo->companyid=$key2;
					$oddsInfo->companyname=$value2;
					$oddsInfo->odds3=$odds3;
					$oddsInfo->odds1=$odds1;
					$oddsInfo->odds0=$odds0;
					array_push($discreteValueArray, 
		    			array(
		    			'Odds3' => $odds3,
		    			'Odds1' => $odds1,
		    			'Odds0' => $odds0,
		    			)
		    		);
		    		$avgOdds3z+=$odds3;
		    		$avgOdds1z+=$odds1;
		    		$avgOdds0z+=$odds0;
					if($key2=='威廉希尔，计算概率'){
						//威廉希尔，计算概率
						$gameInfoList=GameInfo::model()->findAll(
							array(
								'condition'=>'euroodds3w=:euroodds3w and euroodds1w=:euroodds1w and euroodds0w=:euroodds0w and homegoal!=-1 and awaygoal!=-1 and gametime<:gametime',
								'params'=>array(
									':euroodds3w'=>$odds3,
									':euroodds1w'=>$odds1,
									':euroodds0w'=>$odds0,
									':gametime'=>$value->gametime,
									)
								)
							);
						if(isset($gameInfoList)){
							$gameCount=0;
							$winCount=0;
							$drawCount=0;
							$loseCount=0;
							$winprobability=0;
							$drawprobability=0;
							$loseprobability=0;
							foreach ($gameInfoList as $listKey => $listValue) {
								if($listValue->homegoal!=-1){
									$gameCount++;
									if($listValue->homegoal>$listValue->awaygoal){
										$winCount++;
									}
									else if($listValue->homegoal==$listValue->awaygoal){
										$drawCount++;
									}
									else if($listValue->homegoal<$listValue->awaygoal){
										$loseCount++;
									}
								}
							}
							if($gameCount>0){
								$oddsInfo->winprobability=$winprobability=$winCount/$gameCount;
								$oddsInfo->drawprobability=$drawprobability=$drawCount/$gameCount;
								$oddsInfo->loseprobability=$loseprobability=$loseCount/$gameCount;
							}
							$gameInfoObj=GameInfo::model()->find(
									array(
										'condition'=>'issue=:issue and gameindex=:gameindex',
										'params'=>array(
											':issue'=>$value->issue,
											':gameindex'=>$value->gameindex,
											),
										)
									);
							if(isset($gameInfoObj)){
								$gameInfoObj->euroodds3w=$odds3;
								$gameInfoObj->euroodds1w=$odds1;
								$gameInfoObj->euroodds0w=$odds0;
								$gameInfoObj->winprobability=$winprobability;
								$gameInfoObj->drawprobability=$drawprobability;
								$gameInfoObj->loseprobability=$loseprobability;
								if($gameInfoObj->update()){
									printf("$value2 [编号：%s] [威廉希尔胜赔：%s] [威廉希尔平赔：%s] [威廉希尔负赔：%s] [概率胜：%.2f] [概率平：%.2f] [概率负：%.2f]\n", $value->gameindex, $odds3,$odds1,$odds0,$winprobability,$drawprobability,$loseprobability);
								}
								else{
									printf("更新威廉希尔赔率失败 [编号：%s] [威廉希尔胜赔：%s] [威廉希尔平赔：%s] [威廉希尔负赔：%s] [概率胜：%.2f] [概率平：%.2f] [概率负：%.2f]\n", $value->gameindex, $odds3,$odds1,$odds0,$winprobability,$drawprobability,$loseprobability);
								}
							}
						}

						//威廉希尔，计算概率（同比赛类型）
						$gameInfoList=GameInfo::model()->findAll(
							array(
								'condition'=>'euroodds3w=:euroodds3w and euroodds1w=:euroodds1w and euroodds0w=:euroodds0w and homegoal!=-1 and awaygoal!=-1 and gametype=:gametype and gametime<:gametime',
								'params'=>array(
									':euroodds3w'=>$odds3,
									':euroodds1w'=>$odds1,
									':euroodds0w'=>$odds0,
									':gametype'=>$gametype,
									':gametime'=>$value->gametime,
									)
								)
							);
						if(isset($gameInfoList)){
							$gameCount=0;
							$winCount=0;
							$drawCount=0;
							$loseCount=0;
							$winprobability=0;
							$drawprobability=0;
							$loseprobability=0;
							foreach ($gameInfoList as $listKey => $listValue) {
								if($listValue->homegoal!=-1){
									$gameCount++;
									if($listValue->homegoal>$listValue->awaygoal){
										$winCount++;
									}
									else if($listValue->homegoal==$listValue->awaygoal){
										$drawCount++;
									}
									else if($listValue->homegoal<$listValue->awaygoal){
										$loseCount++;
									}
								}
							}
							if($gameCount>0){
								$winprobability=$winCount/$gameCount;
								$drawprobability=$drawCount/$gameCount;
								$loseprobability=$loseCount/$gameCount;
							}
							$gameInfoObj=GameInfo::model()->find(
									array(
										'condition'=>'issue=:issue and gameindex=:gameindex',
										'params'=>array(
											':issue'=>$value->issue,
											':gameindex'=>$value->gameindex,
											),
									)
								);
							if(isset($gameInfoObj)){
								$gameInfoObj->winprobability_samegametype=$winprobability;
								$gameInfoObj->drawprobability_samegametype=$drawprobability;
								$gameInfoObj->loseprobability_samegametype=$loseprobability;
								if($gameInfoObj->update()){
									printf("相同比赛类型概率 $value2 [编号：%s] [威廉希尔胜赔：%s] [威廉希尔平赔：%s] [威廉希尔负赔：%s] [概率胜：%.2f] [概率平：%.2f] [概率负：%.2f]\n", $value->gameindex, $odds3,$odds1,$odds0,$winprobability,$drawprobability,$loseprobability);
								}
								else{
									printf("更新相同比赛类型概率失败 [编号：%s] [威廉希尔胜赔：%s] [威廉希尔平赔：%s] [威廉希尔负赔：%s] [概率胜：%.2f] [概率平：%.2f] [概率负：%.2f]\n", $value->gameindex, $odds3,$odds1,$odds0,$winprobability,$drawprobability,$loseprobability);
								}
							}
						}
					}

					if($key2=='5'){
						//澳门赔率
						$gameInfoObj=GameInfo::model()->find(
							array(
								'condition'=>'issue=:issue and gameindex=:gameindex',
								'params'=>array(
									':issue'=>$value->issue,
									':gameindex'=>$value->gameindex,
									)
								)
							);
						if(isset($gameInfoObj)){
							$gameInfoObj->odds3m=$odds3;
							$gameInfoObj->odds1m=$odds1;
							$gameInfoObj->odds0m=$odds0;
							if($gameInfoObj->update()){
								printf("$value2 [编号：%s] [澳门胜赔：%s] [澳门平赔：%s] [澳门负赔：%s]\n", $value->gameindex, $odds3,$odds1,$odds0);
							}
							else{
								printf("更新澳门赔率失败 [编号：%s] [澳门胜赔：%s] [澳门平赔：%s] [澳门负赔：%s]\n", $value->gameindex, $odds3,$odds1,$odds0);
							}
						}
					}
					$oddsInfo->writetime=date("Y-m-d H:i:s");
					//$oddsInfo->save();
					//printf("$value2 [编号：%s] [胜赔：%s] [平赔：%s] [负赔：%s]\n", $value->gameindex, $odds3,$odds1,$odds0);
		    	}
		    	//计算离散值
				$arrLength=count($discreteValueArray);
				if($arrLength==0){
					$htmlOdds->clear();
					unset($htmlOdds);
					continue;
				}
				$avgOdds3z=$avgOdds3z/$arrLength;
				$avgOdds1z=$avgOdds1z/$arrLength;
				$avgOdds0z=$avgOdds0z/$arrLength;
				$discretevalue3=0;
				$discretevalue1=0;
				$discretevalue0=0;
				foreach ($discreteValueArray as $key => $valueArray) {
					$discretevalue3 = $discretevalue3 + ($valueArray['Odds3'] - $avgOdds3z) * ($valueArray['Odds3'] - $avgOdds3z);
					$discretevalue1 = $discretevalue1 + ($valueArray['Odds1'] - $avgOdds1z) * ($valueArray['Odds1'] - $avgOdds1z);
					$discretevalue0 = $discretevalue0 + ($valueArray['Odds0'] - $avgOdds0z) * ($valueArray['Odds0'] - $avgOdds0z);
				}
				$discretevalue3=sqrt($discretevalue3);
				$discretevalue1=sqrt($discretevalue1);
				$discretevalue0=sqrt($discretevalue0);
				$gameInfoObj=GameInfo::model()->find(
								array(
									'condition'=>'gameid=:gameid',
									'params'=>array(
										':gameid'=>$value->gameid,
										)
									)
								);
				if(isset($gameInfoObj)){
					$gameInfoObj->discretevalue3=$discretevalue3;
					$gameInfoObj->discretevalue1=$discretevalue1;
					$gameInfoObj->discretevalue0=$discretevalue0;
					//if($gameInfoObj->update()){
						//printf("$value->gameid [编号：%s] [离散值3：%.4f] [离散值1：%.4f] [离散值0：%.4f]\n", $value->gameindex, $discretevalue3,$discretevalue1,$discretevalue0);
					//}
				}
		    	$htmlOdds->clear();
				unset($htmlOdds);
			}
			$beginTime=$endTime;
    	}
    }
}